package com.ruoyi.web.controller.jjm;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.jjm.service.*;
import com.ruoyi.jjm.vo.VillageInfo;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jjm.domain.Village;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.servlet.ModelAndView;

/**
 * 村统计信息Controller
 * 
 * @author tsr
 * @date 2020-07-15
 */
@Controller
@RequestMapping("/jjm/village")
public class VillageController extends BaseController
{
    private String prefix = "jjm/village";

    @Autowired
    private IVillageService villageService;
    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private IClientUserService clientUserService;

    @Autowired
    private IGridMemeberService gridMemeberService;

    @Autowired
    private IBaolianrenService baolianrenService;

    @Autowired
    private IClientUserOrderService clientUserOrderService;

    @RequiresPermissions("jjm:village:view")
    @GetMapping()
    public String info()
    {
        return prefix + "/info";
    }
    @GetMapping("/village")
    public String village()
    {
        return prefix + "/village";
    }

    @GetMapping("/mobile")
    public ModelAndView mobile(@RequestParam(value = "id") Long villageId){

        ModelAndView mav = new ModelAndView(prefix + "/villageMobile");

        SysDept villageInfo = deptService.selectDeptById(villageId);

        SysDept xiangInfo = deptService.selectDeptById(villageInfo.getParentId());
        SysDept xianInfo = deptService.selectDeptById(xiangInfo.getParentId());
        mav.addObject("xiangInfo",xiangInfo);
        mav.addObject("xianInfo",xianInfo);
        mav.addObject("villageInfo",villageInfo);
        mav.addObject("villageId",villageId);
        double cunTotalOrder = clientUserOrderService.countCunTotal(villageId);
        mav.addObject("cunTotalOrder",cunTotalOrder);
        mav.addObject("user",ShiroUtils.getSysUser());
        return mav;
    }
    /**
     * 查询村统计信息列表
     */
    @RequiresPermissions("jjm:village:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam(value = "villageId",required = false)Long village)
    {

        SysUser user = ShiroUtils.getSysUser();
        if(village == null){
            village = user.getDeptId();
        }
        startPage();
        VillageInfo villageInfo =  villageService.selectVillageInfoByVillageId(village);
        List<VillageInfo> tableList = new ArrayList<>();
        if(villageInfo != null){
            tableList.add(villageInfo);
        }
        return getDataTable(tableList);
    }
    @PostMapping("/villageList")
    @ResponseBody
    public TableDataInfo villageList(Village village)
    {
        startPage();
        List<Village> list = villageService.selectVillageList(village);
        return getDataTable(list);
    }

    /**
     * 导出村统计信息列表
     */
    @RequiresPermissions("jjm:village:export")
    @Log(title = "村统计信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Village village)
    {
        List<Village> list = villageService.selectVillageList(village);
        ExcelUtil<Village> util = new ExcelUtil<Village>(Village.class);
        return util.exportExcel(list, "village");
    }

    /**
     * 新增村统计信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存村统计信息
     */
    @RequiresPermissions("jjm:village:add")
    @Log(title = "村统计信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Village village)
    {
        return toAjax(villageService.insertVillage(village));
    }

    /**
     * 修改村统计信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Village village = villageService.selectVillageById(id);
        mmap.put("village", village);
        return prefix + "/edit";
    }

    /**
     * 修改保存村统计信息
     */
    @RequiresPermissions("jjm:village:edit")
    @Log(title = "村统计信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Village village)
    {
        return toAjax(villageService.updateVillage(village));
    }

    /**
     * 删除村统计信息
     */
    @RequiresPermissions("jjm:village:remove")
    @Log(title = "村统计信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(villageService.deleteVillageByIds(ids));
    }
}
